<template>
  <div class="shopList">

    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="名称">
        <el-input v-model="formInline.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="formInline.phone" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="formInline.status" placeholder="状态">
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      :data="resList"
      height="500"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="nickname" label="名称"> </el-table-column>
      <el-table-column label="头像">
        <template slot-scope="scope">
          <el-image
            style="width: 40px; height: 40px"
            :src="scope.row.avatar"
            :preview-src-list="srcList"
            @click="srcList.push(scope.row.avatar)"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="mobile" label="手机号"> </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          {{ scope.row.status == 1 ? "启用" : "禁用" }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.created_at | filterTime }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="set(scope.row)"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="del(scope.$index)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="pagechange"
      @current-change="page"
      :current-page="pageNum"
      :page-sizes="[1, 5, 10, 15]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="list.length"
    >
    </el-pagination>

    <el-dialog title="编辑" :visible.sync="flag" width="600px">
      <el-form :model="form">
        <el-form-item label="名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="form.status" placeholder="状态">
            <el-option label="启用" :value="1"></el-option>
            <el-option label="禁用" :value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="flag = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      srcList: [],
      list: [],
      listAll: [],
      form: {
        name: "",
        phone: "",
        status: "",
        id: "",
      },
      pageSize: 5,
      pageNum: 1,
      flag: false,
      formInline: {
        name: "",
        phone: "",
        status: "",
      },
    };
  },
  mounted() {
    this.$axios.get("list.json").then((res) => {
      console.log(res);
      this.list = res.data;
      this.listAll = res.data;
    });
  },
  methods: {
    del(index) {
      this.$confirm("此操作将删除该信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.list.splice(index, 1);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    set(item) {
      this.flag = true;
      this.form.name = item.nickname;
      this.form.phone = item.mobile;
      this.form.status = item.status;
      this.form.id = item.uid;
    },
    update() {
      this.flag = false;
      this.list.forEach((item) => {
        if (item.uid == this.form.id) {
          item.nickname = this.form.name;
          item.mobile = this.form.phone;
          item.status = this.form.status;
        }
      });
      this.$message({
        message: "修改成功",
        type: "success",
      });
    },
    pagechange(val) {
      this.pageSize = val;
    },
    page(val) {
      this.pageNum = val;
    },
    onSubmit() {
      this.list = this.listAll.filter((item) => {
        if (this.formInline.status == "") {
          return (
            item.nickname.includes(this.formInline.name) &&
            item.mobile.includes(this.formInline.phone)
          );
        } else {
          return (
            item.nickname.includes(this.formInline.name) &&
            item.mobile.includes(this.formInline.phone) &&
            item.status == this.formInline.status
          );
        }
      });
    },
  },
  computed: {
    resList() {
      return this.list.slice(
        this.pageSize * (this.pageNum - 1),
        this.pageSize * this.pageNum
      );
    },
  },
  filters: {
    filterTime(value) {
      var time = new Date(value * 1000);
      var year = time.getFullYear();
      var month = time.getMonth() + 1;
      var date = time.getDate();
      var hour = time.getHours();
      var mis = time.getMinutes();
      var sec = time.getSeconds();
      return `${year}-${month
        .toString()
        .padStart(2, "0")}-${date
        .toString()
        .padStart(2, "0")} ${hour
        .toString()
        .padStart(2, "0")}:${mis
        .toString()
        .padStart(2, "0")}:${sec.toString().padStart(2, "0")}`;
    },
  },
};
</script>
<style lang="scss" scoped>
</style>